#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main(int argc,char *argv[]){
	int fd;
	char filename[] = "/home/ubuntu/algorithm/test/rf_on.wav";
	if((fd = open(filename,O_RDONLY)) == -1)
	{
		printf("Fail to open!\n");
		return -1;
	}
	int len = lseek(fd,0,SEEK_END);
	printf("len: %d\n",len);
	lseek(fd,0,SEEK_SET);
	unsigned char *buf = new unsigned char[len];

	printf("buf : %ld\n",sizeof(buf));
	int rd;
	int tmp = 0;
	while(1)
{
	if((rd = read(fd,buf+tmp,len-tmp)) == 0)
	{
		printf("finish read\n");
		break;
	}
	printf("Success to read %d\n",rd);
	tmp += rd;
}
	close(fd);

	char target[] = "/home/ubuntu/algorithm/test/new.wav";
		if((fd = open(target,O_RDWR|O_CREAT)) == -1)
	{
		printf("Fail to open!\n");
		return -1;
	}
	int wr;
	tmp = 0;
	while(1)
{
		if((wr = write(fd,buf+tmp,len-tmp)) == 0)
	{
		printf("finish write!\n");
		break;
	}
	printf("Success to write %d\n",wr);
	tmp += wr;
}
	close(fd);
	delete [] buf;
	system("chmod 777 Sort");
	printf("END\n");
}
